package com.ayitubie.refuelweb.config.jenkins;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.net.URI;
import java.net.URISyntaxException;


/**
 * 连接 Jenkins
 */
@Slf4j
@NoArgsConstructor
@Configuration
public class JenkinsConnect {

    // 连接 Jenkins 需要设置的信息

    private static String url;


    private static String username;


    private static String password;

    @Value("${jenkins.url}")
    public void initUrl(String s) {
        url = s;
    }

    @Value("${jenkins.username}")
    public void initUsername(String s) {
        username = s;
    }

    @Value("${jenkins.password}")
    public void initPassword(String s) {
        password = s;
    }

    /**
     * @return
     */
    public static JenkinsHttpClient getClient() {
        JenkinsHttpClient jenkinsHttpClient = null;
        try {
            jenkinsHttpClient = new JenkinsHttpClient(new URI(url), username, password);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return jenkinsHttpClient;
    }

    /**
     * 连接 Jenkins
     */
    public static JenkinsServer connection() {
        JenkinsServer jenkinsServer = null;
        try {
            jenkinsServer = new JenkinsServer(new URI(url), username, password);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return jenkinsServer;
    }
}
